<?php
/**
 * Purpose:
 * 新文件
 * File Name:
 * Order.php
 * Author:
 * 武当山道士<912900700@qq.com>
 * Create:
 * 2019/9/29 19:26
 */

namespace app\api\model;

use think\db;

class Order extends Base
{
    /**
     * 搜索订单
     * @param int $user_id 必须
     * @param int $status null=所有类型
     * @param string $key_word null=不进行关键字匹配，查找所有
     * @return null
     * @throws
     */
    public function searchOrders($user_id, $status=null, $key_word='')
    {
        if ( empty($user_id) ) return null;
        $where = ['o.user_id'=>$user_id];
        if ( !empty($status) ) {
            $where['o.status'] = $status;
        }
        if ( !empty($key_word) ) {
            $where['i.name'] = ['like', "%$key_word%"];
        }
        $list = Db::table('fa_order o')
            ->join('fa_item i','o.item_id=i.id')
            ->field("i.name as item_name, o.id as order_id, FROM_UNIXTIME(o.createtime) as submit_time,
            CONCAT('$this->siteUrl',i.images) as item_image, total_price, o.status as order_status")
            ->where($where)->select();
        return $list ? : null;
    }
}